// Package route
// @Author zhongxc
// @Date 2023/12/14 10:56:00
// @Desc
package route

import (
	"github.com/zeromicro/go-zero/core/logx"
	"net/http"
	"zhongxc.cc/admin-user-api/internal/handler"
	"zhongxc.cc/admin-user-api/internal/middleware"
	"zhongxc.cc/admin-user-api/internal/svc"
)

type RoutesHandler struct {
	routes *Routes
	svcCtx *svc.ServiceContext
}

func NewRoutesHandler(routes *Routes, svcCtx *svc.ServiceContext) *RoutesHandler {
	return &RoutesHandler{
		routes: routes,
		svcCtx: svcCtx,
	}
}

func (r *RoutesHandler) InitRoute() {
	// 菜单模块
	menuHandler := handler.NewMenuHandler(r.svcCtx)
	v1MenuGroup := r.routes.Group("/v1/menu", middleware.NewAuthorizeMiddleware().Handle)
	v1MenuGroup.Get("/list", menuHandler.GetMenuList)
	v1MenuGroup.Get("/list/all", nil)
	v1MenuGroup.Get("/list/select", nil)
	v1MenuGroup.Get("/info", nil)
	v1MenuGroup.Get("/update", nil)
	v1MenuGroup.Get("/create", nil)
	v1MenuGroup.Get("/delete", nil)
	v1MenuGroup.Get("/nav", nil)
	v1MenuGroup.Get("/permissions", nil)

	r.UserHandler()
	r.CommonHandler()
}

func (r *RoutesHandler) UserHandler() {
	user := handler.NewUserHandler(r.svcCtx)

	// 验证码模块
	captchaGroup := r.routes.Group("/v1/captcha")
	captchaGroup.Get("/", user.GenerateCaptchaHandle)
	captchaGroup.Post("/verify", user.CaptchaVerifyHandle)

	// 用户登录
	loginGroup := r.routes.Group("/v1")
	loginGroup.Post("/login", user.Login)

	// 用户管理
	userGroup := r.routes.Group("/v1", middleware.NewAuthorizeMiddleware().Handle)
	userGroup.Get("/user/profile", user.Info)

}

func (r *RoutesHandler) CommonHandler() {
	uploadHandler := handler.NewUploadHandler(r.svcCtx)

	// 上传文件
	uploadGroup := r.routes.Group("/v1", uploadMiddleware)
	uploadGroup.Post("/upload", uploadHandler.Upload)

	uploadGroup.Get("/uuid", uploadHandler.GetUuid)
}

func uploadMiddleware(next http.HandlerFunc) http.HandlerFunc {
	return func(writer http.ResponseWriter, request *http.Request) {
		writer.Header().Add("X-Middleware", "uploadMiddleware")
		logx.Info("上传文件中间件")
		next(writer, request)
	}
}
